<?php 
/**
 * 货品列表控制器
 * @author 杨帆
 */
class GoodsListController extends AuthController{
    private $model;
    /**
     * 自动运行
     */ 
    public function __init(){
        $this->model = K('GoodsList');
    }
    /* 
     * 默认方法
     */
    public function index(){
         
        $gid = Q('get.gid',0,'intval');
        
        //外层是添加货品列表的标题的数据，内层循环是添加商品规格的下拉菜单；
        $addData = M()->join('__goods_attr__ ga JOIN __attribute__ at ON ga.attrid=at.attrid')->where("goods_gid={$gid} AND type=2")->group('title')->order('at.attrid ASC')->all();
        foreach ($addData as $k => $v) {
            $addData[$k]['value'] = M('goods_attr')->where("attrid={$v['attrid']} AND goods_gid={$gid}")->all();
        }
        $this->assign('addData',$addData);
        //显示当前商品的货品列表数据
        $showData = M('goods_list')->where("goods_gid={$gid}")->all();
        foreach ($showData as $k => $v) {
            $showData[$k]['value'] = M('goods_attr')->where("gaid in({$v['gaid']})")->all();
        }
        $this->assign('showData',$showData);
        //视图显示
        $this->display(); 
        
    }
    
    /**
     * 新增
     */
    public function addData(){
        if(IS_POST){
//          p($_POST);die;
            $gid = $_POST['goods_gid'];
            $this->model->addData();
            $this->success('添加成功',U('index',array('gid' => "{$gid}")));
        }
   
//      $this->display();
    }
    /**
    * 删除 
    */ 
    public function del(){
        //通过get获取商品id
        $glid = Q('get.glid',0,'intval');
        $gid = Q('get.gid',0,'intval');
        //判断是否有相应的订单已使用当前货品列表
        
        //删除分类
        $this->model->where("glid={$glid}")->delete();
        //1.提示信息 2.跳转地址 3.跳转时间
        $this->success('删除成功',U('index',array('gid' => "{$gid}")),0.2);
    }
    /**
     * 编辑
     */
    public function edit(){
        if(IS_POST){
            $glid = Q('get.glid',0,'intval');
            $gid = Q('get.gid',0,'intval');
            $this->model->edit($glid);
            $this->success('修改成功',U('index',array('gid'=>"{$gid}")));
        }
        //接入get数据:$gid是商品id；$glid是货品列表id
        $gid = Q('get.gid',0,'intval');
        $glid = Q('get.glid',0,'intval');
        //显示当前商品的货品列表数据
        //外层是添加货品列表的标题的数据，内层循环是添加商品规格的下拉菜单；
        $addData = M()->join('__goods_attr__ ga JOIN __attribute__ at ON ga.attrid=at.attrid')->where("goods_gid={$gid} AND type=2")->group('title')->order('at.attrid ASC')->all();
        foreach ($addData as $k => $v) {
            $addData[$k]['value'] = M('goods_attr')->where("attrid={$v['attrid']} AND Goods_gid={$gid}")->all();
        }
        $this->assign('addData',$addData);
        //分配旧数据；
        $oldData = $this->model->where("glid={$glid}")->find();
        $oldData['gaid'] = explode(',', $oldData['gaid']);
        $this->assign('oldData',$oldData);
        $this->display();
    }
 }
 
 
 
 
 
 
 
 
 
 
 



 ?>